package com.example.demo.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;

@RestController //Controller+ResponseBody
@RequestMapping("two")
public class TwoController {

//	@RequestMapping(value="post",method=RequestMethod.POST)
	@GetMapping(value="post",params= {"age=45"},headers= {"Accept=text/html,application/xhtml+xml,application/x"})
	public String testPost() {
		return "<h1>只允许Post访问，否则会出现405错误</h1>";
	}
	
	
	@RequestMapping("/aaa/**/a.html")
	public String testTong() {
		return "<h1>你的想法是对的，就是这样的</h1>";
	}
	
	
	@RequestMapping("abc/name/{mame}/age/{age}")
	public String testPathValue(@PathVariable("mame") String mame,
			@PathVariable("age") String age) {
		return String.format("<h1>名字叫做：%s,年龄是:%s</h1>",mame,age);
	}
	@GetMapping("param")
	public String testParam(@RequestParam(value="info1",required=true,defaultValue="天空真蓝") String info) {
		return "<h1>"+info+"</h1>";
	}
	
	/**
	 * 请求头的注入
	 * @param hh
	 * @return
	 */
	
	@GetMapping("header1")
	public String getHeader1(@RequestHeader("Accept") String hh) {
		
		return "<h1>"+hh+"</h1>";
		
	}
	
	@GetMapping("cookie")
	public String getCookie(@CookieValue("JSESSIONID") String js) {
		return "<h1>"+js+"<h1>";
	}
	
	@Resource
	private UserService userService;
	
	@RequestMapping("user")
	public Object doUser(User user) {
		return userService.insert(user);
	}
	
	
}
